package com.webserver.core;

import com.webserver.template_core.Creator;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class WebServer {
    private ServerSocket server;
    private ExecutorService threadPool;

    public WebServer(){
        System.out.println("开启服务端......");
        try {
            server = new ServerSocket(8088);
            //创建线程池，给定50线程数
            threadPool = Executors.newFixedThreadPool(50);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("服务端以开启!");
    }

    private void start(){
        try {

            while (true){
                System.out.println("等待客户端连接......");
                Socket socket = server.accept();
                ClientHandler handler = new ClientHandler(socket);
                //启动线程
                threadPool.execute(handler);
                System.out.println("一个客户端连接了！");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Creator creator = new Creator();
        creator.start();
        WebServer server = new WebServer();
        server.start();
    }
}
